using System;
using System.Text;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Toon.Models.Characters;

namespace Toon.Models.Tests.Characters
{
	
	public partial class PossessionCollectionTests
	{


        

        
        
		[TestMethod]
        public void PossessionCollectionTests_ListT_Possession_AddClear_Memory ()
		{
			IList<Toon.Models.Characters.Possession> objectUnderTest = CreateObject();

			if (objectUnderTest.IsReadOnly)
                return;

			Func<WeakReference> run = () =>
			{
				Toon.Models.Characters.Possession newItem;

				try
				{
					newItem = new Toon.Models.Characters.Possession(new Toon.Models.Catalogs.PossessionDefinition("A", default(Toon.Models.Catalogs.PossessionType), "A"));
					objectUnderTest.Add(newItem);
					return new WeakReference(newItem);
				}
				catch (ArgumentException)
				{
					return null;
				}            
			};

			var wr = run();
			if (wr == null)
				return; //test inconclusive

			//release strong references
			objectUnderTest.Clear();
			
            GC.Collect();
			GC.WaitForPendingFinalizers();
			GC.Collect();

			//verify the object was collected
			Assert.IsFalse(wr.IsAlive);

		}


        
        
        [TestMethod]
        public void PossessionCollectionTests_ListT_Possession_AddRemove_Memory ()
		{
			IList<Toon.Models.Characters.Possession> objectUnderTest = CreateObject();

			if (objectUnderTest.IsReadOnly)
                return;

            var oldCount = objectUnderTest.Count;
            
			Func<WeakReference> run = () =>
			{
				Toon.Models.Characters.Possession newItem;

				try
				{
					newItem = new Toon.Models.Characters.Possession(new Toon.Models.Catalogs.PossessionDefinition("A", default(Toon.Models.Catalogs.PossessionType), "A"));
					objectUnderTest.Add(newItem);
					return new WeakReference(newItem);
				}
				catch (ArgumentException)
				{
					return null;
				}            
			};

			var wr = run();
			if (wr == null)
				return; //test inconclusive

			//release strong references
			Action run2 = () => {
				objectUnderTest.Remove((Toon.Models.Characters.Possession)wr.Target);
			};
			
			run2();

            GC.Collect();
			GC.WaitForPendingFinalizers();
			GC.Collect();

			//verify the object was collected
			Assert.IsFalse(wr.IsAlive);		
        }

        
        
        [TestMethod]
        public void PossessionCollectionTests_ListT_Possession_AddRemoveAt_Memory ()
		{
			IList<Toon.Models.Characters.Possession> objectUnderTest = CreateObject();

			if (objectUnderTest.IsReadOnly)
                return;

			Func<WeakReference> run = () =>
			{
				Toon.Models.Characters.Possession newItem;

				try
				{
					newItem = new Toon.Models.Characters.Possession(new Toon.Models.Catalogs.PossessionDefinition("A", default(Toon.Models.Catalogs.PossessionType), "A"));
					objectUnderTest.Add(newItem);
					return new WeakReference(newItem);
				}
				catch (ArgumentException)
				{
					return null;
				}            
			};

			var wr = run();
			if (wr == null)
				return; //test inconclusive

			//release strong references
			//release strong references
			Action run2 = () => {
				var newIndex = objectUnderTest.IndexOf((Toon.Models.Characters.Possession)wr.Target);
				objectUnderTest.RemoveAt(newIndex);
			};
			
			run2();
			
            GC.Collect();
			GC.WaitForPendingFinalizers();
			GC.Collect();

			//verify the object was collected
			Assert.IsFalse(wr.IsAlive);		
		}


        
	}
}

